<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */
//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;

require_once __DIR__ . '/Server/MajongServer.php';
require_once __DIR__ . '/Server/MsgRouteServer.php';
require_once __DIR__ . '/Server/NiuniuServer.php';
require_once __DIR__ . '/Server/PublicServer.php';
require_once __DIR__ . '/Server/RouterMsg.php';
require_once __DIR__ . '/Server/ServerResponse.php';
require_once __DIR__ . '/Channel/Client.php';
require_once __DIR__ . '/Channel/Server.php';


/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
class Events
{
    private static $majongServer;
    private static $niuniuServer;
    private static $publicServer;
    private static $routeServer;
    private static $serverResponse;
    /**
     * 当某个Worker启动的时候触发
     * @param $worker
     */
    public static function onWorkerStart($worker){
        //消息转发服务器，每个进程都维持了一个
        self::$routeServer = new MsgRouteServer();
        self::$serverResponse = new ServerResponse();
        //绑定Channel
        Channel\Client::connect();
        echo "进程ID".$worker->id;
        switch($worker->id){
            //如果是进程0，则注册成为消息分发服务
            case 0:
                self::$publicServer = new PublicServer();
                //做什么工作
                Channel\Client::on(RouterMsg::$PUBLIC_EVENT, function($event_data){
                    //print_r($event_data);
                    //echo "注册Channel,Public\n";
                    $r = self::$publicServer->processPublicRequest($event_data);
                    for($i = 0; $i < count($r); $i++){
                        self::$serverResponse->processServerResponse($r[$i][0],$r[$i][1],$r[$i][2],$r[$i][3],$r[$i][4]);
                    }
                });
                break;
            //如果是进程1，则注册成处理麻将信息的专用进程
            case 1:
                self::$majongServer = new MajongServer();
                //处理麻将的请求，注册麻将消息
                Channel\Client::on(RouterMsg::$MAJONG_EVENT, function($event_data){
                    //print_r($event_data);
                    //echo "注册Channel,Public\n";
                    $r = self::$majongServer->processMajongRequest($event_data);
                    for($i = 0; $i < count($r); $i++){
                        self::$serverResponse->processServerResponse($r[$i][0],$r[$i][1],$r[$i][2],$r[$i][3],$r[$i][4]);
                    }
                });
                break;
            //如果是进程2，则注册成处理牛牛信息的专用进程
            case 2:
                self::$niuniuServer = new NiuniuServer();
                //处理牛牛的请求，注册牛牛消息
                Channel\Client::on(RouterMsg::$NIUNIU_EVENT, function($event_data){
               //     print_r($event_data);
               //     echo "注册Channel,Public\n";
                   $r = self::$niuniuServer->processNiuniuRequest($event_data);
                    for($i = 0; $i < count($r); $i++){
                        self::$serverResponse->processServerResponse($r[$i][0],$r[$i][1],$r[$i][2],$r[$i][3],$r[$i][4]);
                    }
                });
                break;
        }
    }
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id) {
        echo "连接".$client_id."\n";
//        // 向当前client_id发送数据
//        Gateway::sendToClient($client_id, "Hello $client_id\n");
//        // 向所有人发送
//        Gateway::sendToAll("$client_id login\n");
    }
    
   /**
    * 当客户端发来消息时触发
    * @param int $client_id 连接id
    * @param mixed $message 具体消息
    */
   public static function onMessage($client_id, $message) {
       //echo "消息转发".$client_id."\n";
       //收到客户端消息，任意进程都可以进行消息转发
       self::$routeServer->msgRouteProcess($client_id, $message, $_SERVER['REMOTE_ADDR']);
   }
   
   /**
    * 当用户断开连接时触发
    * @param int $client_id 连接id
    */
   public static function onClose($client_id) {
       // 向所有人发送 
       GateWay::sendToAll("$client_id logout");
   }


}
